package com.enjoy.service.impl;

import com.enjoy.domain.VdsInfoDevice;
import com.enjoy.mapper.VdsInfoDeviceMapper;
import com.enjoy.service.VdsInfoDeviceService;
import com.enjoy.util.SnowflakeGenerateIdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 姜风
 * @date 2021/9/7 14:49
 */
@Service
@Slf4j
public class VdsInfoDeviceServiceImpl implements VdsInfoDeviceService {

    @Autowired
    private VdsInfoDeviceMapper vdsInfoDeviceMapper;

    /**
     * 查询启用的设备列表
     *
     * @param i 启用状态
     * @return 返回
     */
    @Override
    public List<String> findDeviceByEnable(int i) {
        return vdsInfoDeviceMapper.findDeviceByEnable(i);
    }

    /**
     * 注册设备
     *
     * @param device 设备
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void registerVdsInfoDevice(VdsInfoDevice device) {
        // 根据设备编号查询设备
        VdsInfoDevice oldDevice = vdsInfoDeviceMapper.getDeviceByDeviceId(device.getDeviceId());
        // 判断该设备是否已经注册过
        if (oldDevice != null) {
            oldDevice.setRegisterTime(device.getRegisterTime());
            oldDevice.setStatus(device.getStatus());
            oldDevice.setIp(device.getIp());
            vdsInfoDeviceMapper.updateById(oldDevice);
        } else {
            device.setId(SnowflakeGenerateIdUtil.getFlowIdInstance().nextId());
            vdsInfoDeviceMapper.insert(device);
        }
    }

    /**
     * 根据设备编号查询设备信息
     *
     * @param deviceId 设备编号
     * @return 设备
     */
    @Override
    public VdsInfoDevice getDeviceByDeviceId(String deviceId) {
        return vdsInfoDeviceMapper.getDeviceByDeviceId(deviceId);
    }

    /**
     * 根据主键更新
     *
     * @param device 设备
     * @return 返回
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(VdsInfoDevice device) {
        return vdsInfoDeviceMapper.updateById(device);
    }

    /**
     * 获取设备列表
     * @return 设备列表
     */
    @Override
    public List<VdsInfoDevice> getDeviceList() {
        return vdsInfoDeviceMapper.getDeviceList();
    }
}
